2 Copyright (C) 2024 Rubén Beltrán del Río
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see https://map.tranquil.systems.
19 struct MapVertices: View {
22 let vertexSize: CGSize
23 let vertices: [Vertex]
24 let padding = CGFloat(5.0)
26 var onDragVertex: (Vertex, CGFloat, CGFloat) -> Void = { _, _, _ in }
29 ZStack(alignment: .topLeading) {
30 ForEach(vertices, id: \.id) { vertex in
31 ZStack(alignment: .topLeading) {
32 getVertexShape(vertex).fill(Color.map.vertexColor)
33 Text(vertex.label.replacingOccurrences(of: "\\n", with: "\n")).font(.theme.vertexLabel)
34 .foregroundColor(.map.labelColor)
35 .shadow(color: .white, radius: 0, x: -0.5, y: -0.5)
36 .shadow(color: .white, radius: 0, x: 0.5, y: 0.5)
39 width: w(vertex.position.x) + vertexSize.width + padding,
40 height: h(vertex.position.y) + 7.0))
44 let deltaX = value.startLocation.x - value.location.x
45 let deltaY = value.startLocation.y - value.location.y
46 onDragVertex(vertex, deltaX, deltaY)
53 func h(_ dimension: CGFloat) -> CGFloat {
54 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
57 func w(_ dimension: CGFloat) -> CGFloat {
58 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
61 func getVertexShape(_ vertex: Vertex) -> Path {
67 origin: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y)), size: vertexSize
74 x: w(vertex.position.x), y: h(vertex.position.y), width: vertexSize.width,
75 height: vertexSize.height
80 path.move(to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
83 x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y) + vertexSize.height)
86 to: CGPoint(x: w(vertex.position.x) + vertexSize.width / 2.0, y: h(vertex.position.y)))
88 to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
93 path.move(to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y)))
96 x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y) + vertexSize.height)
99 path.move(to: CGPoint(x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y)))
101 to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
103 }.strokedPath(StrokeStyle(lineWidth: 2.0, lineCap: .butt))
110 mapSize: CGSize(width: 400.0, height: 400.0), vertexSize: CGSize(width: 25.0, height: 25.0),
112 Vertex(id: 0, label: "A Circle", position: CGPoint(x: 50.0, y: 50.0)),
113 Vertex(id: 1, label: "A Square", position: CGPoint(x: 10.0, y: 20.0), shape: .square),
114 Vertex(id: 2, label: "A triangle", position: CGPoint(x: 25, y: 32.0), shape: .triangle),
115 Vertex(id: 3, label: "An X", position: CGPoint(x: 70.0, y: 70.0), shape: .x),